package _80

import (
	. "GoLeetcode/common"
)

/*
时间复杂度：O(N)
空间复杂度：O(N)

hash
*/

func minimumOperations(nums []int) int {
	if len(nums) <= 1 {
		return 0
	}
	hashEven := make(map[int]int)
	hashOdd := make(map[int]int)
	n := len(nums)
	firstMaxOdd, firstCountOdd, secondCountOdd := 0, 0, 0
	firstMaxEven, firstCountEven, secondCountEven := 0, 0, 0
	for i := 0; i < n; i += 2 {
		hashOdd[nums[i]]++
		if i+1 < n {
			hashEven[nums[i+1]]++
		}
	}
	for k, v := range hashOdd {
		if firstCountOdd <= v {
			secondCountOdd = firstCountOdd
			firstCountOdd = v
			firstMaxOdd = k
		} else if secondCountOdd < v {
			secondCountOdd = v
		}
	}

	for k, v := range hashEven {
		if firstCountEven <= v {
			secondCountEven = firstCountEven
			firstCountEven = v
			firstMaxEven = k
		} else if secondCountEven < v {
			secondCountEven = v
		}
	}
	oprCount := 0
	if firstMaxOdd != firstMaxEven {
		oprCount = firstCountOdd + firstCountEven
	} else {
		oprCount = Max(firstCountOdd+secondCountEven, secondCountOdd+firstCountEven)
	}
	return n - oprCount
}
